<?php

/**
 * [PHPFOX_HEADER]
 */
defined('PHPFOX') or exit('NO DICE!');

/**
 * 
 * 
 * @copyright		[PHPFOX_COPYRIGHT]
 * @author  		Raymond Benc
 * @package 		Phpfox_Service
 * @version 		$Id: process.class.php 4977 2012-10-31 14:12:43Z Miguel_Espinoza $
 */
class Marketplace_Service_Store_Video extends Phpfox_Service {

    public function __construct() {
        $this->_sTable = Phpfox::getT('marketplace_video');
    }

    public function add($aVals, $iId) {
        $aVideo = array(
            'listing_id' => $iId,
            'title' => isset($aVals["title"]) ? $aVals["title"] : "",
            'description' => isset($aVals["description"]) ? $aVals["description"] : "",
            'embed_code' => isset($aVals["embed_code"]) ? $aVals["embed_code"] : "",
        );
        $this->database()->insert($this->_sTable, $aVideo);
        if (isset($aVals["image"]) && !empty($aVals["image"])) {
            $oImage = Phpfox::getLib("image");
            $sFileName = Phpfox::getLib("file.link")->load($aVals["image"], Phpfox::getParam('marketplace.dir_image_video'), $iId);
            $aSizes = array(50, 120, 200, 400);
            $this->database()->update(Phpfox::getT('marketplace_video'), array('image_path' => $sFileName, 'server_id' => Phpfox::getLib('request')->getServer('PHPFOX_SERVER_ID')),'listing_id = '. $iId);

            foreach ($aSizes as $iSize) {
                $oImage->createThumbnail(Phpfox::getParam('marketplace.dir_image_video') . sprintf($sFileName, ''), Phpfox::getParam('marketplace.dir_image_video'). sprintf($sFileName, '_' . $iSize), $iSize, $iSize);
                $oImage->createThumbnail(Phpfox::getParam('marketplace.dir_image_video') . sprintf($sFileName, ''), Phpfox::getParam('marketplace.dir_image_video') . sprintf($sFileName, '_' . $iSize . '_square'), $iSize, $iSize, false);
            }
            $aVideo["image_path"] = $sFileName;
            $aVideo["server_id"] = Phpfox::getLib('request')->getServer('PHPFOX_SERVER_ID');
        }
        return $aVideo;
    }
    public function getVideo($iListingId){
        $aReturn = $this->database()->select("*")
                ->from(Phpfox::getT("marketplace_video"))
                ->where("listing_id = ".$iListingId)
                ->execute("getRow");
        if(empty($aReturn)){
            return false;
        }
        
        $aReturn["embed_code"] = str_replace('width="400"','width="435"',  $aReturn["embed_code"]);
        $aReturn["embed_code"] = str_replace('height="300"','height="300"',  $aReturn["embed_code"]);
        
        return $aReturn;
    }
}

?>
